package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.*;
import com.database.*;

public class MakeReservationServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	
	String firstName = null;
	String lastName = null;
	String username = null;
	String connecting = null;
	String msg = null;
	
	String sPaidPrice1 = null;
	String sPaidPoint1 = null;
	
	String sPaidPrice2 =null;
	String sPaidPoint2 = null;
	
	int point = -1;
	int numRes = -1;
	int numPointToClaim = -1;

	private void ConnectingFlightReservation(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, SQLException
	{
		String meal1 = req.getParameter("meal1");
		String seat1 = req.getParameter("seat1");
		String flightID1 =  req.getParameter("flightID1");
		
		String meal2 = req.getParameter("meal2");
		String seat2 = req.getParameter("seat2");
		String flightID2 = req.getParameter("flightID2");
		
		
		float paidPrice1 = Float.parseFloat(sPaidPrice1);
		int paidPoint1 = Integer.parseInt(sPaidPoint1);
		
		float paidPrice2 = Float.parseFloat(sPaidPrice2);
		int paidPoint2 = Integer.parseInt(sPaidPoint2);
								
		float price1 = PaymentHelper.GetPriceForSeat(seat1, flightID1);
		float price2 = PaymentHelper.GetPriceForSeat(seat2, flightID2);
		float price = price1 + price2;
		
		float paidPrice = paidPrice1 + paidPrice2;
		int paidPoint = paidPoint1 + paidPoint2;
		
		if (paidPrice + paidPoint < price)//if paid total is less than the price, user will directed to the error page
		{
			msg = "Inadequate Amount";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
		}
		else if (paidPrice + paidPoint > price)//if total paid is greater than the price, user will directed to the er
		{
			msg = "Your paid more than the price. Please go back and adjust your payment";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
		}
		else if (PaymentHelper.HasEnoughPoints(username, paidPoint) == false)// if user does not have enough points, user will directed to the error page
		{
			msg = "Not enough loyalty points";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
		}
		else// user make the payment and reservation added
		{					
			CustomerHelper.ModPoint(username, paidPoint);
			int point = CustomerHelper.GetSelectedCustomer(username).point;
			
			ReservationDB db1 = new ReservationDB(firstName, lastName, paidPrice1, paidPoint1, meal1, seat1, username, flightID1);
			db1.Setup(connection);	
			SeatHelper.MarkReserved(seat1, flightID1);
			db1.Insert();
			
			ReservationDB db2 = new ReservationDB(firstName, lastName, paidPrice2, paidPoint2, meal2, seat2, username, flightID2);
			db2.Setup(connection);	
			SeatHelper.MarkReserved(seat2, flightID2);
			db2.Insert();
			
			ReservationHelper.Setup(connection, username);
			point = CustomerHelper.GetSelectedCustomer(username).point;
			numRes = db1.GetReservationList(username).size();
			numPointToClaim = ReservationHelper.GetNumPointsToClaim();
			req.setAttribute("point", point);
			req.setAttribute("numRes", numRes);
			req.setAttribute("numPointToClaim", numPointToClaim);
			
			msg = "2 new reservations added!";
			req.setAttribute("msg", msg);
			req.setAttribute("username", username);
			req.setAttribute("point", point);
			req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		String msg = null;
		ConnectDB();
		
		firstName = req.getParameter("firstName");
		lastName = req.getParameter("lastName");
		username = req.getParameter("username");
		
		String sPaidPrice;
		String sPaidPoint;
		float paidPrice;
		int paidPoint;
		
		sPaidPrice = req.getParameter("credit");
		sPaidPoint = req.getParameter("point");
		connecting = req.getParameter("connecting");
		
		sPaidPrice1 = req.getParameter("credit1");
		sPaidPoint1 = req.getParameter("point1");
		
		sPaidPrice2 = req.getParameter("credit2");
		sPaidPoint2 = req.getParameter("point2");
				
		try //
		{
			PaymentHelper.Setup(connection);
			CustomerHelper.Setup(connection);
			FlightHelper.Setup(connection);
			
			if(connecting.equals("1"))
			{
				if (NaN(sPaidPrice1) || NaN(sPaidPoint1) || NaN(sPaidPrice2) || NaN(sPaidPoint2))
				{
					msg = "Invalid Number";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else
				{
					ConnectingFlightReservation(req, resp);
				}
			}
			else if (NaN(sPaidPrice) || NaN(sPaidPoint))//if number is invalid, user will directed to the error page
			{
				msg = "Invalid Number";
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
			}
			else
			{
				String meal = null;
				String seat = null;
				String flightID = null;
				
				meal = req.getParameter("meal");
				flightID = req.getParameter("flightID");
				seat = req.getParameter("seat");
				
				paidPrice = Float.parseFloat(sPaidPrice);
				paidPoint = Integer.parseInt(sPaidPoint);
				float price = PaymentHelper.GetPriceForSeat(seat, flightID);
				
				if (paidPrice + paidPoint < price)//if paid totoal is less than the price, user will directed to the error page
				{
					msg = "Inadequate Amount";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else if (paidPrice + paidPoint > price)//if total paid is greater than the price, user will directed to the er
				{
					msg = "Your paid more than the price. Please go back and adjust your payment";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else if (PaymentHelper.HasEnoughPoints(username, paidPoint) == false)// if user does not have enough points, user will directed to the error page
				{
					msg = "Not enough loyalty points";
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else// user make the payment and reservation added
				{					
					CustomerHelper.ModPoint(username, paidPoint);
					int point = CustomerHelper.GetSelectedCustomer(username).point;
					ReservationDB db = new ReservationDB(firstName, lastName, paidPrice, paidPoint, meal, seat, username, flightID);
					db.Setup(connection);
					db.Insert();
					SeatHelper.MarkReserved(seat, flightID);
					
					ReservationHelper.Setup(connection, username);
					point = CustomerHelper.GetSelectedCustomer(username).point;
					//numRes = ReservationHelper.GetReservationList().size();
					numRes = db.GetReservationList(username).size();
					numPointToClaim = ReservationHelper.GetNumPointsToClaim();
					req.setAttribute("point", point);
					req.setAttribute("numRes", numRes);
					req.setAttribute("numPointToClaim", numPointToClaim);
					
					
					
					msg = "1 new reservation added!";
					req.setAttribute("msg", msg);
					req.setAttribute("username", username);
					req.setAttribute("point", point);
					req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);
				}
			}
							
		} 
		catch (SQLException e1) 
		{
			msg = "Reservation Failed.";
			e1.printStackTrace();
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);	
		}
				
		
	}

}

